/* 
 * $Id: string_copy.c,v 1.1 2005-09-17 11:12:03 bacon Exp $
 */

#if defined(__MSTRING)
#include <xp/bas/mstring.h>
#elif defined(__WSTRING)
#include <xp/bas/mstring.h>
#else
#include <xp/bas/string.h>
#endif

#include <xp/bas/memory.h>

xp_size_t xp_strcpy (xp_char_t* buf, const xp_char_t* str)
{
	xp_char_t* org = buf;	
	while ((*buf++ = *str++) != XP_CHAR('\0'));
	return buf - org - 1;
}

xp_size_t xp_strcpyn (xp_char_t* buf, const xp_char_t* str, xp_size_t n)
{
	xp_char_t* org = buf;
	const xp_char_t* end = str + n;

	while (str < end) {
		if ((*buf++ = *str++) == XP_CHAR('\0')) return buf - org - 1;
	}

	return n;
}

xp_size_t xp_strncpy (xp_char_t* buf, const xp_char_t* str, xp_size_t len)
{
	const xp_char_t* end = str + len;
	while (str < end) *buf++ = *str++;
	*buf = XP_CHAR('\0');
	return len;
}

xp_size_t xp_strxcpy (xp_char_t* buf, xp_size_t bsz, const xp_char_t* str)
{
	xp_char_t* p, * p2;

	p = buf;
	p2 = buf + bsz - 1;

	while (p < p2) {
		if (*str == XP_CHAR('\0')) break;
		*p++ = *str++;
	}

	if (bsz > 0) *p = XP_CHAR('\0');
	return p - buf;
}

xp_size_t xp_strxncpy (
	xp_char_t* buf, xp_size_t bsz, const xp_char_t* str, xp_size_t len)
{
	xp_size_t n;

	if (bsz <= 0) return 0;
	if ((n = bsz - 1) > len) n = len;
	xp_memcpy (buf, str, n * xp_sizeof(xp_char_t));
	buf[n] = XP_CHAR('\0');

	return n;

#if 0
	xp_char_t* p, * p2;
	const xp_char_t* end;

	p = buf;
	p2 = buf + bsz - 1;
	end = str + len;

	while (p < p2) {
		if (str >= end) break;
		*p++ = *str++;
	}

	if (bsz > 0) *p = XP_CHAR('\0');
	return p - buf;
#endif
}

